package com.walterweimod.auxiliary_attributes.event;

import com.walterweimod.auxiliary_attributes.capability.ModCapabilities;
import com.walterweimod.auxiliary_attributes.capability.impl.base.ChargeLivingEntityAttributeRangedValueBase;
import com.walterweimod.auxiliary_attributes.damagesource.ModDamageSources;
import com.walterweimod.auxiliary_attributes.damagesource.ModEntityDamageSource;
import com.walterweimod.auxiliary_attributes.mobeffect.ModMobEffects;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.effect.MobEffectInstance;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.boss.enderdragon.EnderDragon;
import net.minecraftforge.event.entity.living.LivingHealEvent;
import net.minecraftforge.event.entity.living.LivingHurtEvent;
import net.minecraftforge.eventbus.api.EventPriority;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;

import java.util.Objects;

import static com.walterweimod.auxiliary_attributes.util.ModCapabilityUtils.*;
import static com.walterweimod.auxiliary_attributes.Auxiliary_attributes.*;

@Mod.EventBusSubscriber
public class BloodFeastEvent {
    @SubscribeEvent(priority = EventPriority.LOWEST)
    public static void onBloodFeastHeal(LivingHealEvent event){
        LivingEntity livingEntity = event.getEntityLiving();
        MobEffectInstance blood_feast = livingEntity.getEffect(ModMobEffects.BLOOD_FEAST.get());

        if (Objects.nonNull(blood_feast)){
            setCapabilityValue(
                    livingEntity,
                    ModCapabilities.BLOOD_POOL_VALUE,
                    bloodPoolValue -> bloodPoolValue.addValue(event.getAmount())
            );

            getLogger().info("角色受到治疗，珠露充能：{}", event.getAmount());
            getLogger().info("目前珠露充能值为：{}", getCapabilityValue(
                    livingEntity,
                    ModCapabilities.BLOOD_POOL_VALUE,
                    ChargeLivingEntityAttributeRangedValueBase::getValue,
                    0.0F
            ));
        }
    }

    @SubscribeEvent
    public static void onBloodFeastHurt(LivingHurtEvent event){
        LivingEntity livingEntity = event.getEntityLiving();
        DamageSource source = event.getSource();
        Entity entity = source.getEntity();

        if (entity instanceof LivingEntity sourceEntity){
            MobEffectInstance blood_feast = sourceEntity.getEffect(ModMobEffects.BLOOD_FEAST.get());

            if (Objects.nonNull(blood_feast)
                    && (!(source instanceof ModEntityDamageSource entitySource)
                    || !entitySource.isBloodPool())){
                DamageSource blood_pool_source = ModDamageSources.getBloodFeastDamageSource(sourceEntity);

                int last_invulnerable_time = livingEntity.invulnerableTime;
                livingEntity.invulnerableTime = 0;

                getLogger().info("玩家触发珠露伤害，伤害值为：{}", getCapabilityValue(
                        sourceEntity,
                        ModCapabilities.BLOOD_POOL_VALUE,
                        ChargeLivingEntityAttributeRangedValueBase::getValue,
                        0.0F
                ));

                if (livingEntity instanceof EnderDragon enderDragon){
                    enderDragon.hurt(enderDragon.head, blood_pool_source, getCapabilityValue(
                            sourceEntity,
                            ModCapabilities.BLOOD_POOL_VALUE,
                            ChargeLivingEntityAttributeRangedValueBase::getValue,
                            0.0F
                    ));
                }else {
                    livingEntity.hurt(blood_pool_source, getCapabilityValue(
                            sourceEntity,
                            ModCapabilities.BLOOD_POOL_VALUE,
                            ChargeLivingEntityAttributeRangedValueBase::getValue,
                            0.0F
                    ));
                }

                livingEntity.invulnerableTime = last_invulnerable_time;

                setCapabilityValue(
                        sourceEntity,
                        ModCapabilities.BLOOD_POOL_VALUE,
                        ChargeLivingEntityAttributeRangedValueBase::resetValue
                );

                getLogger().info("重置珠露充能值为：{}", getCapabilityValue(
                        sourceEntity,
                        ModCapabilities.BLOOD_POOL_VALUE,
                        ChargeLivingEntityAttributeRangedValueBase::getValue,
                        0.0F
                ));
            }
        }
    }
}
